1 /*
2  * Hunt - a framework for web and console application based on Collie using Dlang development
3  *
4  * Copyright (C) 2015-2017  Shanghai Putao Technology Co., Ltd
5  *
6  * Developer: HuntLabs
7  *
8  * Licensed under the Apache-2.0 License.
9  *
10  */
11 
12 module hunt.routing.config;
13 
14 import hunt.routing.define;
15 
16 struct RouteItem
17 {
18     string methods;
19     string path;
20     string route;
21 }
22 
23 struct RouteConfig
24 {
25     RouteItem[] loadConfig(string filename)
26     {
27         import std.stdio;
28 
29         RouteItem[] items;
30 
31         auto f = File(filename);
32 
33         scope(exit)
34         {
35             f.close();
36         }
37 
38         foreach (line; f.byLine)
39         {
40             RouteItem item = this.parseOne(line);
41             if (item.path.length > 0)
42             {
43                 items ~= item;
44             }
45         }
46 
47         return items;
48     }
49 
50     RouteItem parseOne(char[] line)
51     {
52         import std.string : strip;
53         import std.regex;
54         import std.conv;
55 
56         RouteItem item;
57 
58         line = strip(line);
59 
60         // not availabale line return null
61         if (line.length == 0 || line[0] == '#')
62         {
63             return item;
64         }
65 
66         // match example: GET, POST    /users    module.controller.action | staticDir:public
67 		auto matched = line.match(regex(`([^/]+)\s+(/[\S]*?)\s+((staticDir[\:][\w|\/|\\]+)|([\w\.]+))`));
68 
69         if (matched)
70         {
71             item.methods = matched.captures[1].to!string.strip;
72             item.path = matched.captures[2].to!string.strip;
73             item.route = matched.captures[3].to!string.strip;
74         }
75 
76         return item;
77     }
78 }